/**
 * 
 */
package component.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

/**
 * 
 * @author liuboen
 * 
 * since:2012-6-7
 * version:1.0.0
 */
public class QueueReceiver {

	@Test
	public void test() {
		System.out.println("test");
	}

	@Test
	public void sendQueueMessage() throws JMSException {
		// ConnectionFactory ：连接工厂，JMS 用它创建连接
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
		// JMS 客户端到JMS Provider 的连接
		QueueConnection connection = connectionFactory.createQueueConnection();
		connection.start();
		// Session： 一个发送或接收消息的线程
		QueueSession session = connection.createQueueSession(Boolean.TRUE,
				Session.AUTO_ACKNOWLEDGE);
		// Destination ：消息的目的地;消息发送给谁.
		// 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置
		Destination destination = session.createQueue("my-queue");
		// 消费者，消息接收者
		MessageConsumer consumer = session.createConsumer(destination);
		while (true) {
			TextMessage message = (TextMessage) consumer.receive(1000);
			if (null != message) {
				System.out.println("收到消息：" + message.getText());
			} else
				break;
		}
		session.commit();
		session.close();
		connection.close();
	}

}
